package com.vitabuybuy.tv.ui.mine.follow

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.tabs.TabLayout
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityMyfollowBinding
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.mine.follow.adapter.MyFollowAdapter
import com.vitabuybuy.tv.ui.mine.follow.viewmodel.MyFollowViewModel
import com.vitabuybuy.tv.ui.shopcenter.store.StoreHomeActivity
import kotlinx.android.synthetic.main.activity_myfollow.*

class MyFollowActivity : BaseActivity<MyFollowViewModel, ActivityMyfollowBinding>() {

    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_myfollow
    private var pageNumber = 1
    private var type = "store"

    private val mAdapter by lazy { MyFollowAdapter() }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.my_follow))
        mBinding.viewModel = viewModel
        tabLayout.apply {
            addTab(tabLayout.newTab().setText("店铺").setTag("store"))
            addTab(tabLayout.newTab().setText("达人").setTag("executor"))
        }
        listview.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
    }

    override fun observeData() {
        viewModel.getMyFollowList(tabLayout.getTabAt(0)?.tag as String, pageNumber)
        viewModel.followlist.observe(this, Observer {
            mAdapter.type = if (tabLayout.selectedTabPosition == 0) {
                1
            } else {
                2
            }
            if (pageNumber == 1) {
                mAdapter.setNewData(it)
            } else {
                mAdapter.addData(it)
            }
            if (mAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getMyFollowList(type, pageNumber)
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                mAdapter.removeAt(msg.arg1)
                XToast.success(getString(R.string.cancel_collect_follow))
            }
        }
    }

    override fun eventHandling() {
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                pageNumber = 1
                viewModel.getMyFollowList(tab?.tag as String, pageNumber)
            }

            override fun onTabReselected(p0: TabLayout.Tab?) {
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }
        })
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getMyFollowList(tabLayout.getTabAt(0)?.tag as String, pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getMyFollowList(tabLayout.getTabAt(0)?.tag as String, pageNumber)
            }
        })
        mAdapter.onItemClickListener = {
            if ((tabLayout.getTabAt(0)?.tag as String) == "store") {
                StoreHomeActivity.openActivity(
                    mContext,
                    mAdapter.mData[it].store.id,
                    mAdapter.mData[it].store.name
                )
            } else {
            }
        }
        mAdapter.onDeleteClickListener = {
            if ((tabLayout.getTabAt(0)?.tag as String) == "store") {
                viewModel.collectOrcancel(mAdapter.mData[it].store.id, it, true)
            } else {
                viewModel.collectOrcancelExecutor(mAdapter.mData[it].executor.id, it, true)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, MyFollowActivity::class.java)
        }
    }
}
